/* eslint-disable no-return-assign */
'use strict'

const Controller = require('egg').Controller

class HomeController extends Controller {
    async index() {
        const { ctx } = this
        await ctx.render('index', {
            name: '这是一个后台输出的值',
            time: +(new Date()),
            csrf: this.ctx.csrf,
        })
    }
    async news() {
        const { ctx } = this
        const params = ctx.params
        ctx.body = params
        console.log(params)
    }
    async content() {
        const { ctx } = this
        const query = ctx.query
        console.log(query)
        ctx.body = query
    }
    async user() {
    // eslint-disable-next-line no-unused-vars
        const userInfo = await this.service.user.getUserInfo()
        this.ctx.body = userInfo
    }
    async foo() {
        this.ctx.body = this.ctx.helper.fromatTime(+(new Date()))
    }
    async add() {
        const post = this.ctx.request.body
        this.ctx.body = post
    }
    async setCookie() {
        this.ctx.cookies.set('username', 'vipbic')
        this.ctx.body = '设置cookie'
    }
    async getCookie() {
        const username = this.ctx.cookies.get('username')
        this.ctx.body = username
    }
    async setSession() {
        this.ctx.session.userage = 25
        this.ctx.body = '设置session'
    }
    async getSession() {
        const userage = this.ctx.session.userage
        this.ctx.body = userage
    }
    async getId() {
        const data = await this.service.user.find()
        this.ctx.body = data
    }
    async creat() {
        const data = await this.service.user.creat()
        this.ctx.body = data
    }
    async send() {
        const { ctx, app } = this
        const nsp = app.io.of('/')

        const msg = '{"id":2, "message":666}'

        const data = await JSON.parse(msg)

        // app.io.controllers.chat(msg)
        nsp.emit('chat', data)
        return ctx.body = 'hi, egg'
    }
}

module.exports = HomeController
